package com.alex.common.persistent;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.cfg.Environment;
import org.hibernate.connection.C3P0ConnectionProvider;
import org.hibernate.connection.ConnectionProviderFactory;
import org.hibernate.util.PropertiesHelper;
import org.hibernate.util.ReflectHelper;

import com.mchange.v2.c3p0.DataSources;
import com.mchange.v2.c3p0.PoolConfig;

@SuppressWarnings("deprecation")
public class C3P001ConnectionProvider extends C3P0ConnectionProvider {
	public C3P001ConnectionProvider() {
	}

	public Connection getConnection() throws SQLException {
		/* 35 */Connection c = ds.getConnection();
		/* 36 */if (isolation != null)
			/* 36 */c.setTransactionIsolation(isolation.intValue());
		/* 37 */if (c.getAutoCommit() != autocommit)
			/* 37 */c.setAutoCommit(autocommit);
		/* 38 */return c;
	}

	public void closeConnection(Connection conn) throws SQLException {
		/* 42 */conn.close();
	}

	public void configure(Properties props) throws HibernateException {
		/* 46 */String jdbcDriverClass = props
				.getProperty("hibernate.connection.driver_class");
		/* 47 */String jdbcUrl = props.getProperty("hibernate.connection.url");
		/* 48 */Properties connectionProps = ConnectionProviderFactory
				.getConnectionProperties(props);
		/* 50 */log.info("C3P0 using driver: " + jdbcDriverClass + " at URL: "
				+ jdbcUrl);
		/* 51 */log.info("Connection properties: "
				+ PropertiesHelper.maskOut(connectionProps, "password"));
		/* 53 */autocommit = PropertiesHelper.getBoolean(
				"hibernate.connection.autocommit", props);
		/* 54 */log.info("autocommit mode: " + autocommit);
		/* 56 */if (jdbcDriverClass == null)
			/* 57 */log
					.warn("No JDBC Driver class was specified by property hibernate.connection.driver_class");
		/* 61 */else
			/* 61 */try {
				/* 61 */Class.forName(jdbcDriverClass);
			}
			/* 63 */catch (ClassNotFoundException cnfe) {
				/* 65 */try {
					/* 65 */ReflectHelper.classForName(jdbcDriverClass);
				}
				/* 67 */catch (ClassNotFoundException e) {
					/* 68 */String msg = "JDBC Driver class not found: "
							+ jdbcDriverClass;
					/* 69 */log.fatal(msg, e);
					/* 70 */throw new HibernateException(msg, e);
				}
			}
		/* 77 */try {
			/* 77 */int minPoolSize = PropertiesHelper.getInt(
					"hibernate.c3p0.min_size", props, 1);
			/* 78 */int maxPoolSize = PropertiesHelper.getInt(
					"hibernate.c3p0.max_size", props, 100);
			/* 79 */int maxIdleTime = PropertiesHelper.getInt(
					"hibernate.c3p0.timeout", props, 0);
			/* 80 */int maxStatements = PropertiesHelper.getInt(
					"hibernate.c3p0.max_statements", props, 0);
			/* 81 */int acquireIncrement = PropertiesHelper.getInt(
					"hibernate.c3p0.acquire_increment", props, 1);
			/* 82 */int idleTestPeriod = PropertiesHelper.getInt(
					"hibernate.c3p0.idle_test_period", props, 0);
			/* 84 */PoolConfig pcfg = new PoolConfig();
			/* 85 */pcfg.setInitialPoolSize(minPoolSize);
			/* 86 */pcfg.setMinPoolSize(minPoolSize);
			/* 87 */pcfg.setMaxPoolSize(maxPoolSize);
			/* 88 */pcfg.setAcquireIncrement(acquireIncrement);
			/* 89 */pcfg.setMaxIdleTime(maxIdleTime);
			/* 90 */pcfg.setMaxStatements(maxStatements);
			/* 91 */pcfg.setIdleConnectionTestPeriod(idleTestPeriod);

			// **最大空闲时间,N秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0**
			// pcfg.setMaxIdleTime(10);
			// pcfg.setTestConnectionOnCheckout(true);

			/* 96 */DataSource unpooled = DataSources.unpooledDataSource(
					jdbcUrl, connectionProps);
			/* 97 */ds = DataSources.pooledDataSource(unpooled, pcfg);
		}
		/* 100 */catch (Exception e) {
			/* 101 */log.fatal("could not instantiate C3P0 connection pool", e);
			/* 102 */throw new HibernateException(
					"Could not instantiate C3P0 connection pool", e);
		}
		/* 105 */String i = props.getProperty("hibernate.connection.isolation");
		/* 106 */if (i == null) {
			/* 107 */isolation = null;
		} else {
			/* 110 */isolation = new Integer(i);
			/* 111 */log.info("JDBC isolation level: "
					+ Environment.isolationLevelToString(isolation.intValue()));
		}
	}

	public void close() {
		/* 118 */try {
			/* 118 */DataSources.destroy(ds);
		}
		/* 120 */catch (SQLException sqle) {
			/* 121 */log.warn("could not destroy C3P0 connection pool", sqle);
		}
	}

	public boolean supportsAggressiveRelease() {
		/* 129 */return false;
	}

	static Class<?> _mthclass$(String x0) throws Throwable {
		/* 32 */try {
			/* 32 */return Class.forName(x0);
		}
		/* 32 */catch (ClassNotFoundException x1) {
			/* 32 */throw (new NoClassDefFoundError()).initCause(x1);
		}
	}

	private DataSource ds;
	private Integer isolation;
	private boolean autocommit;
	private static final Log log;

	static {
		/* 32 */log = LogFactory
				.getLog(org.hibernate.connection.C3P0ConnectionProvider.class);
	}
}
